﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using weikebuluo.DBUtility;

namespace weikebuluo.DAL
{
    public partial class w_vlimg_dal
    {
        DataCreator<weikebuluo.Model.w_vlimg_model> CreateSQL = new DataCreator<weikebuluo.Model.w_vlimg_model>();

        public w_vlimg_dal()
        { }

        #region 增加一条数据
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(weikebuluo.Model.w_vlimg_model model)
        {
            SqlParameter[] parameters = CreateSQL.CreateParameter(model);

            string strSql = CreateSQL.CreateInsertSql(model, "w_vlimg", "vl_id") + ";select @@IDENTITY";

            object obj = DbHelperSQL.GetSingle(strSql, parameters);

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 增加一条数据(存储过程)
        /// </summary>
        public int AddPro(weikebuluo.Model.w_vlimg_model model)
        {
            int rowsAffected;

            SqlParameter[] parameters = {
					new SqlParameter("@outResult", SqlDbType.Int,4),
					new SqlParameter("@vl_img", SqlDbType.VarChar,250),
					new SqlParameter("@mid", SqlDbType.VarChar,50),
					new SqlParameter("@vl_title", SqlDbType.VarChar,250),
					new SqlParameter("@vl_description", SqlDbType.VarChar,250),
					new SqlParameter("@vl_wechat_name", SqlDbType.VarChar,250),
					new SqlParameter("@vl_wechat_openid", SqlDbType.VarChar,50),
					new SqlParameter("@vl_type", SqlDbType.VarChar,50)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.vl_img;
            parameters[2].Value = model.mid;
            parameters[3].Value = model.vl_title;
            parameters[4].Value = model.vl_description;
            parameters[5].Value = model.vl_wechat_name;
            parameters[6].Value = model.vl_wechat_openid;
            parameters[7].Value = model.vl_type;

            DbHelperSQL.RunProcedure("pro_vimglog", parameters, out rowsAffected);

            return (int)parameters[0].Value;
        }
        #endregion 增加一条数据

        #region 获得数据列表
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM w_vlimg");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion 获得数据列表


        #region 获取记录总数
        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(1) FROM w_vlimg ");

            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }

            object obj = DbHelperSQL.GetSingle(strSql.ToString());

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        #endregion 获取记录总数
    }
}
